package main

import (
	"bytes"
	"fmt"
	"log"
	"time"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/hashicorp/consul/api/watch"
	"github.com/spf13/viper"
	_ "github.com/spf13/viper/remote"
)

var (
	defaultConfig *viper.Viper
	consulAddress string
	consulPath    string
)

func initConfig() *viper.Viper {
	consulAddress = "http://47.98.136.126:8500"
	//consulPath = "config/v1/local"
	//consulPath = "cmp/resource/yacmp:cloudentry:type:openstack/iaas.machine/abcaliyun-config"

	consulPath = "cloud_entry/openstack"

	defaultConfig = viper.New()
	defaultConfig.SetConfigType("json")

	consulClient, err := consulapi.NewClient(&consulapi.Config{Address: consulAddress})
	if err != nil {
		log.Fatalln("consul连接失败:", err)
	}

	kv, _, err := consulClient.KV().Get(consulPath, nil)
	if err != nil {
		log.Fatalln("consul获取配置失败:", err)
	}

	err = defaultConfig.ReadConfig(bytes.NewBuffer(kv.Value))
	if err != nil {
		log.Fatalln("Viper解析配置失败:", err)
	}

	go watchConfig()

	return defaultConfig
}
func watchConfig() {
	time.Sleep(time.Second * 10)
	params := make(map[string]interface{})
	params["type"] = "key"
	params["key"] = consulPath
	fmt.Println("consulPath", consulPath) //cmp/resource/yacmp:cloudentry:type:openstack/iaas.machine/abcaliyun-config
	fmt.Println("params", params)
	w, err := watch.Parse(params)
	if err != nil {
		log.Fatalln(err)
	}
	w.Handler = func(u uint64, i interface{}) {
		kv := i.(*consulapi.KVPair)
		hotconfig := viper.New()
		hotconfig.SetConfigType("json")
		err = hotconfig.ReadConfig(bytes.NewBuffer(kv.Value))
		if err != nil {
			log.Fatalln("Viper解析配置失败:", err)
		}
		defaultConfig = hotconfig
		fmt.Println("defaultConfig", defaultConfig)
	}
	err = w.Run(consulAddress)
	if err != nil {
		log.Fatalln("监听consul错误:", err)
	}
}
func GetConfig() *viper.Viper {
	if defaultConfig == nil {
		defaultConfig = initConfig()
	}

	return defaultConfig
}
func configs() {
	//res:=ReadOne()
	//for k,v:=range *res{
	//	fmt.Println("xxx",k,v)
	//}
	//fmt.Println("res",*res)

	//go func() {
	//	for {
	//				host := GetConfig().GetString("ecs_access_key")
	//				fmt.Println("consul===:", host)
	//				time.Sleep(time.Second * 10)
	//			}
	//	fmt.Println("xxxxxxxxxxxxxxxxxx")
	//
	//}()
	//
	//select {}
}

func ReadOne() map[string]string {
	runtimeConfig := viper.New()
	runtimeConfig.AddRemoteProvider("consul", "http://47.98.136.126:8500", "cloud_entry/openstack")
	//runtimeConfig.SetConfigName("abcaliyun-config")
	runtimeConfig.SetConfigType("json")
	err := runtimeConfig.ReadRemoteConfig()
	if err != nil {
		log.Fatalln("viper read:", err)
	}
	err = runtimeConfig.WatchRemoteConfigOnChannel()
	if err != nil {
		log.Fatalln("viper watch err:", err)
	}
	m := make(map[string]string)
	//go func() {
	//	for {
	cloud_entry_id := runtimeConfig.GetString("cloud_entry_id")

	ecs_access_key := runtimeConfig.GetString("ecs_access_key")


	ecs_secret_access_key := runtimeConfig.GetString("ecs_secret_access_key")
	fmt.Println("ecs_secret_access_key=====:", ecs_secret_access_key)


	m["cloud_entry_id"] = cloud_entry_id
	m["ecs_access_key"] = ecs_access_key
	m["ecs_secret_access_key"] = ecs_secret_access_key

	//	}
	//
	//}()

	//fmt.Println("m",m)
	return m

}
